#!/bin/bash
# Prompt the user to enter the name of their GPU
echo "What is your gpu? amd, nouveau, nvidia, intel, ati"
read name
# If the user's GPU is AMD, install the necessary packages
if [ $name == "amd" ]; then
    sudo pacman -S --needed mesa lib32-mesa xf86-video-amdgpu vulkan-radeon lib32-vulkan-radeon libva-mesa-driver lib32-libva-mesa-driver mesa-vdpau lib32-mesa-vdpau
    echo 'Section "OutputClass"
     Identifier "AMD"
     MatchDriver "amdgpu"
     Driver "amdgpu"
     Option "EnablePageFlip" "off"
     Option "TearFree" "false"
EndSection' > /etc/X11/xorg.conf.d/20-amdgpu.conf
    # yay -S --noconfirm amf-amdgpu-pro
    echo "low" > /sys/class/drm/card1/device/power_dpm_force_performance_level
    echo "auto" > /sys/class/drm/card1/device/power_dpm_force_performance_level
    echo "high" > /sys/class/drm/card1/device/power_dpm_force_performance_level
    echo "5" > /sys/class/drm/card1/device/pp_power_profile_mode
    echo 'SUBSYSTEM=="pci", DRIVER=="amdgpu", ATTR{power_dpm_force_performance_level}="low"
SUBSYSTEM=="pci", DRIVER=="amdgpu", ATTR{pp_power_profile_mode}="3"' > /etc/udev/rules.d/30-amdgpu-low-power.rules
    udevadm control --reload
    udevadm trigger
    echo "s 7 1000 750" > /sys/class/drm/card1/device/pp_od_clk_voltage

    watch -n 0.5  cat /sys/kernel/debug/dri/1/amdgpu_pm_info
fi

# If the user's GPU is Nouveau, remove the NVIDIA packages and install the necessary packages
if [ $name == "nouveau" ]; then
    sudo pacman -Rc nvidia nvidia-open nvidia-lts nvidia-beta nvidia-settings nvidia-utils egl-wayland
    pacman -S --needed mesa lib32-mesa xf86-video-nouveau libva-mesa-driver mesa-vdpau libva-vdpau-driver vulkan-nouveau lib32-vulkan-nouveau
    # Add nouveau.config=NvGspRm=1 nouveau.config=NvBios=PRAMIN kernekl parameter
    echo 'LIBVA_DRIVER_NAME=nouveau' >> /etc/environment
    # echo 'LIBVA_DRIVER_NAME=vdpau' >> /etc/environment
fi

# If the user's GPU is NVIDIA, install the necessary packages
if [ $name == "nvidia" ]; then
    pacman -S --noconfirm --needed nvidia nvidia-utils lib32-nvidia-utils libva-nvidia-driver # new gpu
    yay -S --noconfirm --needed linux-headers nvidia-470xx-dkms lib32-nvidia-470xx-utils libva-nvidia-driver nvidia-470xx-settings # kepler
    #set ibt=off nvidia_drm.modeset=1 nvidia_drm.fbdev=1 kernel parameter. the nvidia_drm.modeset=1 option must be set through kernel parameters
    echo 'options nvidia_drm modeset=1
options nvidia_drm fbdev=1' > /etc/modprobe.d/nvidia.conf
    #Remove kms from the HOOKS array in /etc/mkinitcpio.conf and regenerate the initramfs.
    # add nvidia nvidia_modeset nvidia_uvm nvidia_drm to the initramfs.If you use mkinitcpio initramfs, follow mkinitcpio#MODULES to add modules.
    echo 'LIBVA_DRIVER_NAME=nvidia' >> /etc/environment
fi

# If the user's GPU is ATI, install the necessary packages
if [ $name == "ati" ]; then
    pacman -S --noconfirm --needed mesa lib32-mesa xf86-video-ati libva-mesa-driver lib32-libva-mesa-driver mesa-vdpau lib32-mesa-vdpau
fi

# If the user's GPU is Intel, install the necessary packages
if [ $name == "intel" ]; then
    pacman -S --noconfirm --needed mesa lib32-mesa vulkan-intel lib32-vulkan-intel intel-media-driver libva-intel-driver # libvdpau-va-gl libva-vdpau-driver # broadwell and newer
    pacman -S --needed mesa lib32-mesa vulkan-intel lib32-vulkan-intel libva-intel-driver # old
fi

